package com.yunxin.admin.system.infra.common.util;

import cn.hutool.json.JSONUtil;

import com.yunxin.common.result.Result;
import com.yunxin.common.result.ResultCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 响应工具类
 *
 * @author zero°
 * @since 2023-05-22
 */
public class ResponseUtils {

    /**
     * 异常消息返回(适用过滤器中处理异常响应)
     *
     * @param response response
     * @param resultCode resultCode
     */
    public static void writeErrMsg(HttpServletResponse response, ResultCode resultCode) throws IOException {
        switch (resultCode) {
            case ACCESS_UNAUTHORIZED, TOKEN_INVALID -> response.setStatus(HttpStatus.UNAUTHORIZED.value());
            case TOKEN_ACCESS_FORBIDDEN -> response.setStatus(HttpStatus.FORBIDDEN.value());
            default -> response.setStatus(HttpStatus.BAD_REQUEST.value());
        }
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(JSONUtil.toJsonStr(Result.failed(resultCode)));
    }


}
